home *** CD-ROM | disk | FTP | other *** search
- From: Eugene Lazutkin <eugene@int.com>
- Message-ID: <01BAF15D.08B8EEE0@dino.int.com>
- X-Original-Date: Fri, 2 Feb 1996 10:55:57 -0600
- Path: in1.uu.net!bounce-back
- Date: 03 Feb 96 06:36:31 GMT
- Approved: fjh@cs.mu.oz.au
- Organization: -
- Newsgroups: comp.std.c++
- Subject: RE: Cleaning auto_ptr copy semantics.
- Encoding: 91 TEXT
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMRMChOEDnX0m9pzZAQHMqQF+PH9+UUUF58laJsOi7McxjVAMqsJbiLcF
- 116iHkKpAdpq8EVD5Z7sEEvPI10Wq/e7
- =eJjv
-
- On Friday, January 26, 1996 9:47 AM, gregor@netcom.com (Greg Colvin) wrote:
-
- > A simple implementation makes the semantics clear:
- >
- > template<class X> class auto_ptr {
- > X* px;
- > mutable bool owner;
- > public:
- > explicit auto_ptr(X* p=0) : px(p), owner(true) {}
- > template<class Y>
- > auto_ptr(const auto_ptr<Y>& r) : px(r.release()), owner(true) {}
- > template<class Y>
- > auto_ptr& operator=(const auto_ptr<Y>& r) {
- > if (&r != this) {
- > if (owner) delete px; else owner = true;
- > px = r.release();
- > }
- > return *this;
- > }
- > ~auto_ptr() { if (owner) delete px; }
- >
- > X& operator*() const { return *px; }
- > X* operator->() const { return px; }
- > X* get() const { return px; }
- > X* release() const { owner = false; return px; }
- > };
-
- Sometimes I implement copy-op?
-
- template<class Y>
- auto_ptr& operator=( Y* p ) {
- if( p != px ) {
- if( owner )
- delete px;
- else
- owner = true;
- px = p;
- }
- }
-
- Of course this is just model suggestion. I'm not sure about constness of
- a pointer. Maybe const pointers suggest that auto_ptr doesn't take ownership.
- Anyway this is just a question.
-
- Sometimes I need reallocate my object and it looks like:
-
- auto_ptr<MYOBJECT> x( new MYOBJECT(1) );
- /* ... */
- x = auto_ptr<MYOBJECT>( new MYOBJECT(1) );
-
- If x keeps a head of dynamic structure, I need to change the value of the x
- when this dynamic structure is changing (growing or shrinking). To my the
- current auto_ptr (and your proposal) doesn't provide an elegant solution.
- IMHO, the stright simple assignment operator will work just fine.
-
- I didn't find standard equal-operators:
-
- template<class T>
- bool operator== ( const auto_ptr<T>& l, const T* r )
- { return l.get()==r; }
-
- template<class T>
- bool operator== ( const T* l, const auto_ptr<T>& r )
- { return l==r.get(); }
-
- template<class T>
- bool operator== ( const auto_ptr<T>& l, const auto_ptr<T>& r )
- { return l.get()==r.get(); }
-
- template<class T>
- bool operator!= ( const auto_ptr<T>& l, const T* r )
- { return l.get()!=r; }
-
- template<class T>
- bool operator!= ( const T* l, const auto_ptr<T>& r )
- { return l!=r.get(); }
-
- template<class T>
- bool operator!= ( const auto_ptr<T>& l, const auto_ptr<T>& r )
- { return l.get()!=r.get(); }
-
- It's pretty convinient. It's good if you are going to create a STL
- container of auto_ptr's. In this case you can use STL's algorithms.
- Otherwise each programmer should implement this functions manually.
-
- Best regards
-
-
-
- Eugene Lazutkin
- eugene@int.com
- ---
- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
- Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
- is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
-